﻿namespace Sklad.WindowsApplication
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Windows.Data;
    using DAO;

    public partial class AdminMainForm : Form
    {
        public static DaoRepository dr = new DaoRepository();

        public AdminMainForm()
        {
            InitializeComponent();
            this.bindingSource1.DataSource = new BindingList<Employer>(dr.EmployeeRepository.GetAll().ToList());
            this.bindingNavigator1.BindingSource = bindingSource1;
            tb_FIO.DataBindings.Add("text",bindingSource1,"FIO");
            this.tb_pas_num.DataBindings.Add("text", bindingSource1, "Passport_Number");
            this.tb_pas_ser.DataBindings.Add("text", bindingSource1, "Passport_Ser");
            this.tb_password.DataBindings.Add("text", bindingSource1, "Password");
            this.tb_login.DataBindings.Add("text", bindingSource1, "Login");
            this.is_admin.DataBindings.Add("checked", bindingSource1, "Is_Admin");
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            AddUserForm adf = new AddUserForm();
            adf.FormClosing += adf_FormClosing;
            adf.Visible = true;
            this.Visible = false;
        }

        private void adf_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = true;
            UpdateData();
        }

        public void UpdateData()
        {
            this.bindingSource1.DataSource = new BindingList<Employer>(dr.EmployeeRepository.GetAll().ToList());
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            var temp = new Employer()
            {
                Id = (this.bindingSource1.Current as Employer).Id,
                FIO = this.tb_FIO.Text,
                Passport_Number = this.tb_pas_num.Text,
                Passport_Ser = this.tb_pas_ser.Text,
                Login = this.tb_login.Text,
                Password = this.tb_password.Text,
                Is_Admin = this.is_admin.Checked,
            };
            dr.EmployeeRepository.Edit(temp);
            UpdateData();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            dr.EmployeeRepository.Delete((this.bindingSource1.Current as Employer).Id);
            UpdateData();
        }
    }
}
